﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace AggregateRoot.Console
{
    class AggregateRootWithChildOperation2
    {
        #region Add Supplier -- Add (only) Child tables (with primary key)

        //CRUD -- Child Data

        public static void Add_Supplier_Add_Child_Product_Data_That_Already_Exists()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = new Supplier();
            supp.CompanyName = "Microsoft Corporation 001";
            Product p = new ProductRepository(db).GetByProductId(1);

            supp.Products.Add(p);//update the product foreign key

            suppRep.Add(supp);
            suppRep.Save();
        }
    
        public static void Add_Supplier_Add_Child_Product_Data_That_Dont_Exists()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = new Supplier();
            supp.CompanyName = "Microsoft Corporation 003";
            Product p = new Product();
            p.ProductName = "new-product-002";
            supp.Products.Add(p);//inserts a new row
            
            suppRep.Add(supp);
            suppRep.Save();
        }

        #endregion

        #region Update Supplier -- CRUD Child Tables (with primary key)

        public static void Update_Supplier_Add_Child_Product_Data_That_Dont_Exists()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = suppRep.GetBySupplierId(36);
            
            Product p = new Product();
            p.ProductName = "new-product-002-09";
            supp.Products.Add(p);//inserts a new row

            //suppRep.Add(supp);
            suppRep.Save();
        }

        public static void Update_Supplier_Remove_Child_Product_That_Makes_FK_Ref_Null_In_Child_Table()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = suppRep.GetBySupplierId(36);

            Product p = supp.Products.SingleOrDefault(p1 => p1.ProductID == 88);

            supp.Products.Remove(p);//makes the supplier id/fk as null. throws exception if allow null is false
            //..//if a composite key child table the row would be physically deleted!

            suppRep.Save();
        }

        #endregion

        #region FK pointer update

        public static void Update_Supplier_Update_Child_Product_FK_By_Child()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = suppRep.GetBySupplierId(36);

            Product p = supp.Products.SingleOrDefault(p1 => p1.ProductID == 87);
            p.Supplier.SupplierID = 1;//fk can't be updated!!

            suppRep.Save();
        }

        public static void Update_Supplier_Update_Child_Product_FK_By_Parent()
        {
            DataContext db = new DataContext();
            SupplierRepository suppRep = new SupplierRepository(db);
            Supplier supp = suppRep.GetBySupplierId(36);
            Supplier supp2 = suppRep.GetBySupplierId(1);

            Product p = supp.Products.SingleOrDefault(p1 => p1.ProductID == 87);
            //p.Supplier.SupplierID = 1;//fk can't be updated
            supp2.Products.Add(p);//updating via parent

            suppRep.Save();
        }

        #endregion
    }

}

